﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

using PanGu;
using PanGu.Dict;
using MySql.Data;
using MySql.Data.MySqlClient;

namespace Seg
{
    public partial class Form1 : Form
    {
        String DBConnStr;
        DataSet MyDataSet;
        MySqlDataAdapter DataAdapter;
        MySqlConnection myConnection;

        public Form1()
        {
            InitializeComponent();
            initMysql();
        }

        private void addNode(WordInfo wordinfo)
        {
            TreeNode node = new TreeNode();
            node.Text = wordinfo.Word.ToString();
            treeView1.Nodes.Add(node);
        }

        private void clearNodes()
        {
            treeView1.Nodes.Clear();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            clearNodes();
            int count = 0;
            PanGu.Segment.Init();
            Segment segment = new Segment();
            ICollection<WordInfo> words = segment.DoSegment(textBox1.Text);
            StringBuilder wordString = new StringBuilder();
            foreach (WordInfo wordinfo in words)
            {
                if (wordinfo == null) continue;
                //检查wordinfo.Word是否存在于数据库
                MySqlCommand myCommand = new MySqlCommand("SearchLabel", myConnection);
                myCommand.CommandType = CommandType.StoredProcedure;
                //传参
                myCommand.Parameters.AddWithValue("?Label", wordinfo.Word);
                //存储过程的输出@Contain，1为存在，0为不存在
                myCommand.Parameters.Add("?isExisting", MySqlDbType.Bit);
                myCommand.Parameters["?isExisting"].Direction = ParameterDirection.Output;

                myCommand.ExecuteNonQuery();
                DataAdapter.SelectCommand = myCommand;

                if (int.Parse(myCommand.Parameters["?isExisting"].Value.ToString()) == 0) continue;

                if (count == 10) continue;
                count++;
                addNode(wordinfo);
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (treeView1.Nodes.Count == 0)
                return;
            if (treeView1.SelectedNode == null)
                return;
            string url = "http://zh.wikipedia.org/wiki/" + treeView1.SelectedNode.Text;
            System.Diagnostics.Process.Start(url);
        }

        private void button3_Click(object sender, EventArgs e)
        {
            //对选中的节点查数据库找到它的父亲，每个父亲作为一个node
            MySqlCommand myCommand = new MySqlCommand("SearchLabel", myConnection);
            myCommand.CommandType = CommandType.StoredProcedure;
            //传参
            myCommand.Parameters.AddWithValue("?Label", treeView1.SelectedNode.Text);
            myCommand.Parameters.Add("?isExisting", MySqlDbType.Bit);
            myCommand.Parameters["?isExisting"].Direction = ParameterDirection.Output;
            //存储过程的输出为一张只有一列表，每一行是一个词语，代表父亲
            MySqlDataReader reader = myCommand.ExecuteReader();
            while(reader.Read())
            {
                TreeNode node = new TreeNode();
                node.Text = reader[0].ToString();
                treeView1.SelectedNode.Nodes.Add(node);
            }
        }

        private void initMysql()
        {
            MyDataSet = new DataSet();
            DataAdapter = new MySqlDataAdapter();
            DBConnStr = "Server=localhost;Database=rsp;Uid=rsp_user;Pwd=123456;charset=utf8;";
            myConnection = new MySqlConnection(DBConnStr);
            if (myConnection.State != ConnectionState.Open)
            {
                myConnection.Open();
            }
        }

    }
}
